


// Author:					HungNM
// Created:					16-08-2012
// Last Modified:			16-08-2012
//
	
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using eProject.Data;
	
namespace eProject.Business
{
	
	public class SeatAvailable
	{

		#region Constructors

	    public SeatAvailable()
		{}
	    
	
	    public SeatAvailable(
			long scheduleId, 
			DateTime departureDate, 
			int seatClassId) 
		{
	        GetSeatAvailable(
				scheduleId, 
				departureDate, 
				seatClassId); 
	    }

		#endregion

		#region Private Properties

		private long id;
		private long scheduleId;
		private DateTime departureDate;
		private int seatClassId = -1;
		private int totalSeat = -1;
		private DateTime createdDate = DateTime.UtcNow;
		private DateTime lastUpdate = DateTime.UtcNow;
		
		#endregion

		#region Public Properties

		public long Id 
		{
			get { return id; }
			set { id = value; }
		}
		public long ScheduleId 
		{
			get { return scheduleId; }
			set { scheduleId = value; }
		}
		public DateTime DepartureDate 
		{
			get { return departureDate; }
			set { departureDate = value; }
		}
		public int SeatClassId 
		{
			get { return seatClassId; }
			set { seatClassId = value; }
		}
		public int TotalSeat 
		{
			get { return totalSeat; }
			set { totalSeat = value; }
		}
		public DateTime CreatedDate 
		{
			get { return createdDate; }
			set { createdDate = value; }
		}
		public DateTime LastUpdate 
		{
			get { return lastUpdate; }
			set { lastUpdate = value; }
		}

		#endregion

		#region Private Methods

		/// <summary>
		/// Gets an instance of SeatAvailable.
		/// </summary>
		/// <param name="scheduleId"> scheduleId </param>
		/// <param name="departureDate"> departureDate </param>
		/// <param name="seatClassId"> seatClassId </param>
		private void GetSeatAvailable(
			long scheduleId, 
			DateTime departureDate, 
			int seatClassId) 
		{
			try
			{
				using(IDataReader reader = DBSeatAvailable.GetOne(
					scheduleId, 
					departureDate, 
					seatClassId)) 
				{
					PopulateFromReader(reader);
				}
			}
			catch(Exception ex)
			{
				throw ex;
			}
		}
		
		
		private void PopulateFromReader(IDataReader reader) 
		{
			try
			{
				if(reader.Read())
				{
					this.id = Convert.ToInt64(reader["Id"]);
					this.scheduleId = Convert.ToInt64(reader["ScheduleId"]);
					this.departureDate = Convert.ToDateTime(reader["DepartureDate"]);
					this.seatClassId = Convert.ToInt32(reader["SeatClassId"]);
					this.totalSeat = Convert.ToInt32(reader["TotalSeat"]);
					this.createdDate = Convert.ToDateTime(reader["CreatedDate"]);
					this.lastUpdate = Convert.ToDateTime(reader["LastUpdate"]);
				
				}
			}
			catch(Exception ex)
			{
				throw ex;
			}
		}
		
		/// <summary>
        /// Persists a new instance of SeatAvailable. Returns true on success.
        /// </summary>
        /// <returns></returns>
		private bool Create()
		{ 
			int newID = 0;
			
			try
			{
				newID = DBSeatAvailable.Create(
                    this.scheduleId,
                    this.departureDate,
                    this.seatClassId,
                    this.totalSeat,
                    this.createdDate,
                    this.lastUpdate);
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return (newID > 0);
		}

		
		/// <summary>
        /// Updates this instance of SeatAvailable. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
		private bool Update()
		{
			bool IsUpdated = false;
			try
			{
				IsUpdated = DBSeatAvailable.Update(
					this.id, 
					this.scheduleId, 
					this.departureDate, 
					this.seatClassId, 
					this.totalSeat, 
					this.createdDate, 
					this.lastUpdate); 
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return IsUpdated;
		}

		#endregion

		#region Public Methods

		/// <summary>
        /// Saves this instance of SeatAvailable. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
		public bool Save()
		{
			bool IsSaved = false;
			try
			{
				if(this.id > 0)
				{
					IsSaved = Update();
				}
				else
				{
					IsSaved = Create();
				}
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return IsSaved;
		}
		
		#endregion

		#region Static Methods

		/// <summary>
		/// Deletes an instance of SeatAvailable. Returns true on success.
		/// </summary>
		/// <param name="scheduleId"> scheduleId </param>
		/// <param name="departureDate"> departureDate </param>
		/// <param name="seatClassId"> seatClassId </param>
		/// <returns>bool</returns>
		public static bool Delete(
			long  scheduleId, 
            DateTime  departureDate, 
            int  seatClassId) 
		{
			bool IsDeleted = false;
			try
			{
				IsDeleted = DBSeatAvailable.Delete(
					scheduleId, 
					departureDate, 
					seatClassId); 
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return IsDeleted;
		}
		
		
		/// <summary>
		/// Gets a count of SeatAvailable. 
		/// </summary>
		public static int GetCount()
		{
			int count = 0;
			try
			{
				count = DBSeatAvailable.GetCount();
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return count;
		}
	
		private static List<SeatAvailable> LoadListFromReader(IDataReader reader)
		{
			List<SeatAvailable> seatAvailableList = new List<SeatAvailable>();
			try
			{
				while (reader.Read())
				{
					SeatAvailable seatAvailable = new SeatAvailable();
					seatAvailable.id = Convert.ToInt64(reader["Id"]);
					seatAvailable.scheduleId = Convert.ToInt64(reader["ScheduleId"]);
					seatAvailable.departureDate = Convert.ToDateTime(reader["DepartureDate"]);
					seatAvailable.seatClassId = Convert.ToInt32(reader["SeatClassId"]);
					seatAvailable.totalSeat = Convert.ToInt32(reader["TotalSeat"]);
					seatAvailable.createdDate = Convert.ToDateTime(reader["CreatedDate"]);
					seatAvailable.lastUpdate = Convert.ToDateTime(reader["LastUpdate"]);
					seatAvailableList.Add(seatAvailable);
					
				}
			}
			finally
			{
				reader.Close();
			}
	
			return seatAvailableList;
		}
	
		/// <summary>
		/// Gets an IList with all instances of SeatAvailable.
		/// </summary>
		public static List<SeatAvailable> GetAll()
		{
			List<SeatAvailable> listSeatAvailable = new List<SeatAvailable>();
			try
			{
				IDataReader reader = DBSeatAvailable.GetAll();
				listSeatAvailable = LoadListFromReader(reader);
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return listSeatAvailable;
		}
	
		/// <summary>
		/// Gets an IList with page of instances of SeatAvailable.
		/// </summary>
		/// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="totalPages">total pages</param>
		public static List<SeatAvailable> GetPage(int pageNumber, int pageSize, out int totalPages)
		{
			totalPages = 1;
			List<SeatAvailable> listSeatAvailable = new List<SeatAvailable>();
			try
			{
				IDataReader reader = DBSeatAvailable.GetPage(pageNumber, pageSize, out totalPages);	
				listSeatAvailable = LoadListFromReader(reader);
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return listSeatAvailable;
		}
		
		#endregion

	}
}





